package com.gansu.weather.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import com.gansu.weather.bean.CityNew;

import android.content.Context;
import android.content.SharedPreferences;

public class SpUtil {

	public static boolean saveCity(Context context,CityNew cityNew){
		DataBaseUtil dataBaseUtil = new DataBaseUtil(context);
		dataBaseUtil.open();
		boolean flag = dataBaseUtil.insertData(cityNew);
		dataBaseUtil.closeDataBase();
		return flag;
	}
	
	public static void upDateCity(Context context,CityNew cityNew){
		DataBaseUtil dataBaseUtil = new DataBaseUtil(context);
		dataBaseUtil.open();
		dataBaseUtil.upDate(cityNew);
		dataBaseUtil.closeDataBase();
	}
	
	public static void saveSetting(Context context,String name,String value){
		SharedPreferences sp = context.getSharedPreferences("setting", context.MODE_WORLD_WRITEABLE);
		sp.edit().putString(name, value).commit();
	}
	
	public static void saveSetting(Context context,String name,boolean value){
		SharedPreferences sp = context.getSharedPreferences("setting", context.MODE_WORLD_WRITEABLE);
		sp.edit().putBoolean(name, value).commit();
	}
	
	public static boolean readBooleanSetting(Context context,String name){
		SharedPreferences sp = context.getSharedPreferences("setting", context.MODE_WORLD_WRITEABLE);
		return sp.getBoolean(name, false);
	}
	
	public static String readSetting(Context context,String name){
		SharedPreferences sp = context.getSharedPreferences("setting", context.MODE_WORLD_WRITEABLE);
		return sp.getString(name, null);
	}
	
	public static void delCity(Context context,CityNew cityNew){
		DataBaseUtil dataBaseUtil = new DataBaseUtil(context);
		dataBaseUtil.open();
		dataBaseUtil.deleteByNewsId(cityNew.getCityCode());
		dataBaseUtil.closeDataBase();
	}
	
	public static ArrayList<CityNew> getAllCity(Context context){
		DataBaseUtil dataBaseUtil = new DataBaseUtil(context);
		dataBaseUtil.open();
		 ArrayList<CityNew> cityNews = dataBaseUtil.selectAllData();
		dataBaseUtil.closeDataBase();
		return cityNews;
	}
	
}
